package org.nuplay;

public class NuPlayWiiMoteDevice {
	

	public boolean debug = true;
	private float last_distance = 0;

	
	
	private NuPlayWiiMoteButtons buttons;
	
	static {
		System.loadLibrary("wii_nuplay");
	}
	
	public NuPlayWiiMoteDevice() {
		buttons = new NuPlayWiiMoteButtons();
	}
	
	public NuPlayWiiMoteButtons getButtons() {
		buttons.setButtons(this);
		return buttons;
	}
	
	public boolean connect(int in_wiimote_id) {
		if ((in_wiimote_id > 0) && (in_wiimote_id < 5)) {
			return nConnect(in_wiimote_id);
		} else {
			trace("wrong wiimote_id, must be 1,2,3 or 4.");
			return false;
		}
	}
	
	public float getDistance() {
		float dot1 = this.nGetDotSize(0);
		float dot2 = this.nGetDotSize(1);
		if ((dot1 != -1 ) && (dot2 != -1)) {
			float a = Math.abs(this.nGetDotX(0) - this.nGetDotX(1));
		//	double b = this.nGetDotY(0) - this.nGetDotY(1);
		//	double c = Math.sqrt((a * a)+ (b * b));
		//	System.out.println(Math.abs(a));
			last_distance = a;
		}
		return last_distance;
	
	}
	
	
	public boolean getButtonStatus(int in_button_id) {
		if ((in_button_id >= NuPlayWiiMoteButtons.A) && (in_button_id <= NuPlayWiiMoteButtons.Two)) {
			return nGetButtonStatus(in_button_id);
		} else {
			trace("wrong button_id, must be between " + NuPlayWiiMoteButtons.A + " and " + NuPlayWiiMoteButtons.Two);
			return false;
		}
	}

	//debug
	private void trace(String in_message) {
		if (this.debug) {
			System.out.println("debug:" + this.getClass().getName() + ":" + in_message);
		}
	}
		
	//native functions
	private static native boolean nConnect(int in_wiimote_id);
	private static native boolean nGetButtonStatus(int in_button_id);
	//
	public static native float nGetPitch();
	public static native float nGetRoll();
	//
	public static native float nGetAccelX();
	public static native float nGetAccelY();
	public static native float nGetAccelZ();
	//
	public static native float nGetDotX(int in_dot_index);
	public static native float nGetDotY(int in_dot_index);
	public static native float nGetDotSize(int in_dot_index);
}
